﻿using System;
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class _ISYC_309_10
    {
        public static void ISYC_309_10(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            refin.Linetype = Linetype.Dashed;
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            centre.Linetype = Linetype.DashDot;
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层


            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            double scale = 2; //放大系数

            double oal = Convert.ToDouble(data["oal"]) * scale;
            double dconms = Convert.ToDouble(data["dconms"]) * scale;
            double bd = Convert.ToDouble(data["bd"]) * scale;
            double wta = Convert.ToDouble(data["wta"]) * scale;
            double lpr = Convert.ToDouble(data["lpr"]) * scale;
            double s = Convert.ToDouble(data["s"]) * scale;
            double l = Convert.ToDouble(data["l"]) * scale;

            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + oal + 4, v1.Y);
            Vector2 v3 = new Vector2(v1.X + 2, v1.Y);
            Vector2 v4 = new Vector2(v3.X, v3.Y + dconms / 3.01);
            Vector2 v5 = new Vector2(v3.X + 0.8, v4.Y + 0.8);
            Vector2 v6 = new Vector2(v3.X + oal - lpr, v3.Y + dconms / 2.462);
            Vector2 v7 = new Vector2(v6.X, v3.Y + dconms / 2);
            Vector2 v8 = new Vector2(v7.X + lpr / 8.173, v7.Y);
            Vector2 v9 = new Vector2(v8.X, v3.Y + bd / 1.64);
            Vector2 v10 = new Vector2(v9.X + lpr / 29.647, v3.Y + bd / 2);
            Vector2 v11 = new Vector2(v10.X, v10.Y * -1);
            Vector2 v12 = new Vector2(v9.X, v9.Y * -1);
            Vector2 v13 = new Vector2(v8.X, v8.Y * -1);
            Vector2 v14 = new Vector2(v7.X, v7.Y * -1);
            Vector2 v15 = new Vector2(v6.X, v6.Y * -1);
            Vector2 v16 = new Vector2(v5.X, v5.Y * -1);
            Vector2 v17 = new Vector2(v4.X, v4.Y * -1);
            Vector2 v21 = new Vector2(v3.X + oal, v3.Y);
            Vector2 v22 = new Vector2(v21.X, v21.Y - s);
            Vector2 v23 = new Vector2(v22.X - l / 5.952, v22.Y);
            Vector2 v24 = new Vector2(v23.X, v21.Y - bd / 3.764);
            Vector2 v25 = new Vector2(v24.X - lpr / 20.432, v24.Y - bd / 3.905);
            Vector2 v26 = new Vector2(v25.X - lpr / 8.195, v25.Y);
            Vector2 v27 = new Vector2(v25.X - lpr / 6.69, v25.Y);
            Vector2 v20 = new Vector2(v26.X, v21.Y);
            Vector2 v19 = new Vector2(v27.X, v21.Y);
            Vector2 v18 = new Vector2(v19.X - lpr / 5.883, v10.Y);
            Vector2 v28 = new Vector2(v19.X, v27.Y + bd / 26.698);
            Vector2 v29 = new Vector2(v21.X - l / 24.038, v21.Y);
            Vector2 v30 = new Vector2(v29.X, v22.Y);
            Vector2 v31 = new Vector2(v21.X - l / 7.813, v21.Y);
            Vector2 v32 = new Vector2(v31.X, v22.Y);
            Vector2 v33 = new Vector2(v21.X - l / 2.01, v21.Y);
            Vector2 v34 = new Vector2(v33.X, v22.Y);
            Vector2 a1 = new Vector2(v6.X, v1.Y);
            Vector2 a2 = new Vector2(v21.X - l / 3.324, v1.Y);

            Vector2 v35 = new Vector2(v1.X, v1.Y - oal / 1.478);
            Vector2 v36 = new Vector2(v35.X + oal + 4, v35.Y);
            Vector2 v37 = new Vector2(v35.X + 2, v35.Y);
            Vector2 v38 = new Vector2(v37.X, v37.Y + dconms / 3.01);
            Vector2 v39 = new Vector2(v37.X + 0.8, v38.Y + 0.8);
            Vector2 v40 = new Vector2(v37.X + oal - lpr, v37.Y + dconms / 2.462);
            Vector2 v41 = new Vector2(v40.X, v37.Y + dconms / 2);
            Vector2 v42 = new Vector2(v41.X + lpr / 8.173, v41.Y);
            Vector2 v43 = new Vector2(v42.X, v37.Y + bd / 1.64);
            Vector2 v44 = new Vector2(v43.X + lpr / 29.647, v37.Y + bd / 2);
            Vector2 v45 = new Vector2(v44.X, v44.Y - bd);
            Vector2 v46 = new Vector2(v43.X, v37.Y - bd / 1.64);
            Vector2 v47 = new Vector2(v46.X, v37.Y - dconms / 2);
            Vector2 v48 = new Vector2(v41.X, v47.Y);
            Vector2 v49 = new Vector2(v40.X, v37.Y - dconms / 2.462);
            Vector2 v51 = new Vector2(v37.X, v37.Y - dconms / 3.01);
            Vector2 v50 = new Vector2(v39.X, v51.Y - 0.8);
            Vector2 v52 = new Vector2(v32.X, v36.Y - wta);
            Vector2 v53 = new Vector2(v30.X, v52.Y + l / 9.191);
            Vector2 v54 = new Vector2(v22.X, v53.Y + l / 52.083);
            Vector2 v55 = new Vector2(v54.X, v54.Y + l / 1.776);
            Vector2 v56 = new Vector2(v55.X - l / 17.361, v55.Y + l / 7.911);
            Vector2 v57 = new Vector2(v55.X, v55.Y + l / 4.223);
            Vector2 v58 = new Vector2(v57.X - l / 7.10, v57.Y + l / 44.643);
            Vector2 v59 = new Vector2(v57.X - l / 6.377, v57.Y + l / 13.889);
            Vector2 v60 = fun.Polar(v59, -148 * pi / 180, l / 1.68);
            Vector2 v61 = new Vector2(v60.X - l / 16.447, v60.Y - l / 9.191);
            Vector2 v62 = new Vector2(v61.X - l / 7.716, v61.Y);
            Vector2 v63 = new Vector2(v62.X + l / 21.552, v62.Y - l / 9.615);
            Vector2 v64 = new Vector2(v62.X + l / 22.321, v62.Y - l / 6.313);
            Vector2 v65 = fun.Polar(v64, -30 * pi / 180, l / 1.64);
            Vector2 v66 = new Vector2(v52.X - l / 15.625, v52.Y + l / 9.191);
            Vector2 v67 = new Vector2(v52.X - l / 6.01, v52.Y + l / 1.965);
            Vector2 v68 = new Vector2(v59.X, v44.Y - bd / 4.783);
            Vector2 v69 = new Vector2(v68.X - bd / 2.517, v44.Y);
            Vector2 v70 = fun.Polar(v64, -30 * pi / 180, l / 2.583);
            Vector2 v72 = new Vector2(v45.X + lpr / 1.637, v45.Y);
            Vector2 v71 = new Vector2(v72.X + lpr / 37.8, v70.Y);
            Vector2 v73 = new Vector2(v72.X - lpr / 6.084, v72.Y + bd / 2.877);
            Vector2 v74 = new Vector2(v73.X + lpr / 28.262, v73.Y + bd / 2.11);
            Vector2 v75 = new Vector2(v74.X + lpr / 8.92, v69.Y);
            Vector2 a3 = new Vector2(v41.X, v37.Y);




            //作图
            Line line8 = new Line(v35, v36);
            line8.Layer = centre;
            line8.Linetype = Linetype.DashDot;
            line8.LinetypeScale = 3;
            dxf.AddEntity(line8);

            LwPolyline line9 = new LwPolyline();
            line9.Vertexes.Add(new LwPolylineVertex(v39));
            line9.Vertexes.Add(new LwPolylineVertex(v50));
            line9.Vertexes.Add(new LwPolylineVertex(v51));
            line9.Vertexes.Add(new LwPolylineVertex(v38));
            line9.Vertexes.Add(new LwPolylineVertex(v39));
            line9.Vertexes.Add(new LwPolylineVertex(v40));
            line9.Vertexes.Add(new LwPolylineVertex(v49));
            line9.Vertexes.Add(new LwPolylineVertex(v50));
            fun.PolyFill(dxf, line9, 0.1, contour);

            LwPolyline line10 = new LwPolyline();
            line10.Vertexes.Add(new LwPolylineVertex(v41));
            line10.Vertexes.Add(new LwPolylineVertex(v48));
            line10.Vertexes.Add(new LwPolylineVertex(v47));
            line10.Vertexes.Add(new LwPolylineVertex(v42));
            line10.Vertexes.Add(new LwPolylineVertex(v41));
            fun.PolyFill(dxf, line10, 0.1, contour);

            LwPolyline line11 = new LwPolyline();
            line11.Vertexes.Add(new LwPolylineVertex(v43));
            line11.Vertexes.Add(new LwPolylineVertex(v44));
            line11.Vertexes.Add(new LwPolylineVertex(v45));
            line11.Vertexes.Add(new LwPolylineVertex(v46));
            line11.Vertexes[0].Bulge = Math.Tan(78 * pi / 180 / 4);
            line11.Vertexes[2].Bulge = Math.Tan(78 * pi / 180 / 4);
            fun.PolyFill(dxf, line11, 0.1, contour);

            LwPolyline line12 = new LwPolyline();
            line12.Vertexes.Add(new LwPolylineVertex(v52));
            line12.Vertexes.Add(new LwPolylineVertex(v53));
            line12.Vertexes.Add(new LwPolylineVertex(v54));
            line12.Vertexes.Add(new LwPolylineVertex(v55));
            line12.Vertexes.Add(new LwPolylineVertex(v56));
            line12.Vertexes.Add(new LwPolylineVertex(v57));
            line12.Vertexes.Add(new LwPolylineVertex(v58));
            line12.Vertexes.Add(new LwPolylineVertex(v59));
            line12.Vertexes.Add(new LwPolylineVertex(v60));
            line12.Vertexes.Add(new LwPolylineVertex(v61));
            line12.Vertexes.Add(new LwPolylineVertex(v62));
            line12.Vertexes.Add(new LwPolylineVertex(v63));
            line12.Vertexes.Add(new LwPolylineVertex(v64));
            line12.Vertexes.Add(new LwPolylineVertex(v65));
            line12.Vertexes.Add(new LwPolylineVertex(v66));
            line12.Vertexes.Add(new LwPolylineVertex(v52));
            fun.PolyFill(dxf, line12, 0.1, contour);

            fun.drawcircle(dxf, v67, l / 11.161, 0.1, contour);

            LwPolyline line13 = new LwPolyline();
            line13.Vertexes.Add(new LwPolylineVertex(v70));
            line13.Vertexes.Add(new LwPolylineVertex(v71));
            line13.Vertexes.Add(new LwPolylineVertex(v72));
            line13.Vertexes.Add(new LwPolylineVertex(v45));
            fun.PolyFill(dxf, line13, 0.1, contour);

            LwPolyline line14 = new LwPolyline();
            line14.Vertexes.Add(new LwPolylineVertex(v44));
            line14.Vertexes.Add(new LwPolylineVertex(v69));
            line14.Vertexes.Add(new LwPolylineVertex(v68));
            line14.Vertexes.Add(new LwPolylineVertex(v59));
            fun.PolyFill(dxf, line14, 0.1, contour);

            LwPolyline line15 = new LwPolyline();
            line15.Vertexes.Add(new LwPolylineVertex(v72));
            line15.Vertexes.Add(new LwPolylineVertex(v73));
            line15.Vertexes.Add(new LwPolylineVertex(v74));
            line15.Vertexes.Add(new LwPolylineVertex(v75));
            line15.Vertexes[0].Bulge = Math.Tan(-56 * pi / 180 / 4);
            line15.Vertexes[1].Bulge = Math.Tan(-58 * pi / 180 / 4);
            line15.Vertexes[2].Bulge = Math.Tan(-24 * pi / 180 / 4);
            fun.PolyFill(dxf, line15, 0.1, contour);


            //标注

            double height = 2;
            double arrowsize = 2;
            double exline = 2;

            //2
            fun.dimrot1(dxf, v41, v48, 90, dconms, 1, height, arrowsize, exline, "DCONMS", "", "");

            fun.dimrot1(dxf, v36, v52, -90, dconms, 1, height, arrowsize, exline, "WF", "", "");

            fun.dimrot1(dxf, v51, v54, 180, oal / 2 + dconms / 2, 1, height, arrowsize, exline, "OAL", "", "");

            fun.dimrot1(dxf, v48, v52, 180, oal / 5 * 2.5, 1, height, arrowsize, exline, "LPR", "", "");

            fun.dimrot1(dxf, v72, v52, 180, oal / 2.5, 1, height, arrowsize, exline, "LH", "", "");

            fun.dimrot1(dxf, v42, v57, 0, oal / 4, 1, height, arrowsize, exline, "LDRED", "", "");

            fun.dimrot1(dxf, v44, v45, -90, dconms / 3, 1, height, arrowsize, exline, "BD", "", "");

            fun.dimrot1(dxf, v54, v52, 180, l / 1.2, 1, height, arrowsize, exline, "PDX", "", "");

            fun.dimrot1(dxf, v54, new Vector2(v54.X, v52.Y), -90, dconms / 3, 1, height, arrowsize, exline, "PDY", "", "");

            fun.jdbz2(dxf, new Line(new Vector2(v54.X, v52.Y), fun.Polar(new Vector2(v54.X, v52.Y), 180 * pi / 180, 0.1)), new Line(new Vector2(v54.X, v52.Y), fun.Polar(new Vector2(v54.X, v52.Y), -90 * pi / 180, 0.1)), l, height, arrowsize, exline, "KAPR", "", "");

            fun.jt(dxf, new Vector2(v39.X + (v40.X - v39.X) / 2, v39.Y + (v40.Y - v39.Y) / 2), 135, 9, height, "CCTMS,CCFMS,CZCMS");


            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }


            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}